package de.chronicals.UnlimitedSlots;

import java.io.IOException;

import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.plugin.PluginDescriptionFile;
import org.bukkit.plugin.java.JavaPlugin;
import org.mcstats.MetricsLite;

public class UnlimitedSlots extends JavaPlugin
{
  public UnlimitedSlotsListener nplListener = new UnlimitedSlotsListener(this);

  public void onEnable()
  {
	  try {
		    MetricsLite metrics = new MetricsLite(this);
		    metrics.start();
		} catch (IOException e) {
		    // Failed to submit the stats :-(
		     System.out.println("[METRICS] Failed to submit the stats, try again later!");

		}
	  PlayerCommandPreprocessEvent();
//    getConfig().options().copyDefaults(true);
//    saveConfig();
	  
	  loadConfig();

    getServer().getPluginManager().registerEvents(this.nplListener, this);

  }
    private void PlayerCommandPreprocessEvent() {
//	new UnlimitedSlotsCommands(this);
  }

  public void onDisable()
  {
  }
  
  public void loadConfig(){
	  String useexternalconfig = "Config.useexternal";
	  this.getConfig().addDefault(useexternalconfig, "true");
	  
	  String maxPlayers = "Config.maxPlayers";
	  this.getConfig().addDefault(maxPlayers, 	150);
	  
	  this.saveConfig();
  }
  
  public boolean onCommand(CommandSender sender, Command cmd, String cmdLabel, String[] args){
	  
	  Player p = (Player)sender;
	  
	  if(cmd.getName().equalsIgnoreCase("us")){
		  if(args.length == 2 && p.isOp()){
			  String Configteil = args[0];
			  
			  if(Configteil.equalsIgnoreCase("maxplayers")){
				  
				  int mp;
				  
				  try{
				  mp = Integer.parseInt(args[1]);
				  }catch(Exception e){
					  e.printStackTrace();
					  p.sendMessage(ChatColor.GRAY + " " );
					  p.sendMessage(ChatColor.GRAY + " " );
					  p.sendMessage(ChatColor.GRAY + " " );
					  p.sendMessage(ChatColor.GRAY + " " );
					  p.sendMessage(ChatColor.GRAY + " " );
					  p.sendMessage(ChatColor.GRAY + " " );
					  p.sendMessage(ChatColor.GRAY + " " );
					  p.sendMessage(ChatColor.GRAY + " " );
					  p.sendMessage(ChatColor.GRAY + " " );
					  p.sendMessage(ChatColor.GRAY + " " );
					  p.sendMessage(ChatColor.GRAY + " " );
					  p.sendMessage(ChatColor.GRAY + " " );
					  p.sendMessage(ChatColor.GRAY + " " );
					  p.sendMessage(ChatColor.GRAY + " " );
					  p.sendMessage(ChatColor.GRAY + " " );
					  p.sendMessage(ChatColor.GRAY + " " );
					  PluginDescriptionFile descFile = this.getDescription();
					  sender.sendMessage(ChatColor.GRAY + "[UnlimitedSlots] " + ChatColor.WHITE + "UnlimitedSlots Version " + descFile.getVersion() + " by CHRONICALSde");
					  p.sendMessage(ChatColor.GRAY + " " );
					  p.sendMessage(ChatColor.GRAY + "[UnlimitedSlots] " + ChatColor.RED + "The command /us maxplayers requires a number!");
					  p.sendMessage(ChatColor.GRAY + "[UnlimitedSlots] " + ChatColor.RED + "Use /us maxplayers 10 for 10 Slots");
					  return true;
				  }
				  
				  this.getConfig().set("Config.maxPlayers", mp);
				  this.saveConfig();
				  p.sendMessage(ChatColor.GRAY + " " );
				  p.sendMessage(ChatColor.GRAY + " " );
				  p.sendMessage(ChatColor.GRAY + " " );
				  p.sendMessage(ChatColor.GRAY + " " );
				  p.sendMessage(ChatColor.GRAY + " " );
				  p.sendMessage(ChatColor.GRAY + " " );
				  p.sendMessage(ChatColor.GRAY + " " );
				  p.sendMessage(ChatColor.GRAY + " " );
				  p.sendMessage(ChatColor.GRAY + " " );
				  p.sendMessage(ChatColor.GRAY + " " );
				  p.sendMessage(ChatColor.GRAY + " " );
				  p.sendMessage(ChatColor.GRAY + " " );
				  p.sendMessage(ChatColor.GRAY + " " );
				  p.sendMessage(ChatColor.GRAY + " " );
				  p.sendMessage(ChatColor.GRAY + " " );
				  p.sendMessage(ChatColor.GRAY + " " );
				  p.sendMessage(ChatColor.GRAY + "[UnlimitedSlots] " + ChatColor.WHITE + "Config saved!");
				  this.reloadConfig();
				  p.sendMessage(ChatColor.GRAY + "[UnlimitedSlots] " + ChatColor.WHITE + "Plugin reload complete!");
				  p.sendMessage(ChatColor.GRAY + "[UnlimitedSlots] " + ChatColor.GREEN + "Slot amount changed to " + mp);
				  p.sendMessage(ChatColor.GRAY + "[UnlimitedSlots] " + ChatColor.YELLOW + "Donate here: " + ChatColor.AQUA + "http://CHRONimg.tk/paypal.php");
				  return true;
			  }
			  
		  }else{
			  PluginDescriptionFile descFile = this.getDescription();
			  p.sendMessage(ChatColor.GRAY + " " );
			  p.sendMessage(ChatColor.GRAY + " " );
			  p.sendMessage(ChatColor.GRAY + " " );
			  p.sendMessage(ChatColor.GRAY + " " );
			  p.sendMessage(ChatColor.GRAY + " " );
			  p.sendMessage(ChatColor.GRAY + " " );
			  p.sendMessage(ChatColor.GRAY + " " );
			  p.sendMessage(ChatColor.GRAY + " " );
			  p.sendMessage(ChatColor.GRAY + " " );
			  p.sendMessage(ChatColor.GRAY + " " );
			  p.sendMessage(ChatColor.GRAY + " " );
			  p.sendMessage(ChatColor.GRAY + " " );
			  p.sendMessage(ChatColor.GRAY + " " );
			  p.sendMessage(ChatColor.GRAY + " " );
			  p.sendMessage(ChatColor.GRAY + " " );
			  p.sendMessage(ChatColor.GRAY + " " );
			  sender.sendMessage(ChatColor.GRAY + "[UnlimitedSlots] " + ChatColor.WHITE + "UnlimitedSlots Version " + descFile.getVersion() + " by CHRONICALSde");
			  sender.sendMessage(ChatColor.GRAY + "[UnlimitedSlots] " + ChatColor.YELLOW + "Donate here: " + ChatColor.AQUA + "http://CHRONimg.tk/paypal.php");
			  p.sendMessage(ChatColor.GRAY + "[UnlimitedSlots] " + ChatColor.RED + "WARNING: " + ChatColor.YELLOW + "DO NOT USE ON NITRADO SERVERS!");
			  p.sendMessage(ChatColor.GRAY + " " );
			  p.sendMessage(ChatColor.GRAY + "[UnlimitedSlots] " + ChatColor.RED + "Use /us maxplayers <number> to change your slot amount.");
//			  p.sendMessage("" + args.length);
			  return true;
		  }
	  }
	  
	  return false;
  }
  
}